package com.example.intermediateactivity.adapters;

import android.content.Context;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.intermediateactivity.R;
import com.example.intermediateactivity.entity.CartInfo;
import com.example.intermediateactivity.entity.GoodsInfo;

import java.util.List;

public class CartCountAdapter extends BaseAdapter {

    private Context context;
    private List<CartInfo> list;

    public CartCountAdapter(Context context, List<CartInfo> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return list.get(i).id;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {

            view = LayoutInflater.from(context).inflate(R.layout.activity_cart_goods_record, null);

            holder = new ViewHolder();
            holder.iv_goods_image = view.findViewById(R.id.iv_goods_image);
            holder.goodsName = view.findViewById(R.id.goods_name);
            holder.goodsDesc = view.findViewById(R.id.goods_desc);
            holder.goodsCount = view.findViewById(R.id.goods_count);
            holder.goodsPrice = view.findViewById(R.id.goods_price);
            holder.goodsAmount = view.findViewById(R.id.goods_amount);

            view.setTag(holder);

        } else {
            holder = (ViewHolder) view.getTag();
        }

        CartInfo record = list.get(i);
        GoodsInfo goodsInfo = record.goods;

        holder.iv_goods_image.setImageURI(Uri.parse(goodsInfo.picPath));
        holder.goodsName.setText(goodsInfo.name);
        holder.goodsDesc.setText(goodsInfo.description);
        holder.goodsCount.setText(String.valueOf(record.count));
        holder.goodsPrice.setText(String.valueOf((int) goodsInfo.price));
        holder.goodsAmount.setText(String.valueOf((int) (record.count * goodsInfo.price)));

        return view;
    }

    public static final class ViewHolder {
        public ImageView iv_goods_image;
        public TextView goodsName;
        public TextView goodsDesc;
        public TextView goodsCount;
        public TextView goodsPrice;
        public TextView goodsAmount;
    }
}
